package yongxin.controller.saleSheet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import yongxin.entity.saleSheet.SaleSheetEntity;
import yongxin.service.saleSheet.SaleSheetServiceI;

/**   
 * @Title: Controller
 * @Description: 销售申请单
 * @author zhangdaihao
 * @date 2015-04-09 13:15:49
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/saleSheetController")
public class SaleSheetController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SaleSheetController.class);

	@Autowired
	private SaleSheetServiceI saleSheetService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 销售申请单列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "saleSheet")
	public ModelAndView saleSheet(HttpServletRequest request) {
		return new ModelAndView("yongxin/saleSheet/saleSheetList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(SaleSheetEntity saleSheet,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(SaleSheetEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, saleSheet, request.getParameterMap());
		this.saleSheetService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除销售申请单
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(SaleSheetEntity saleSheet, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		saleSheet = systemService.getEntity(SaleSheetEntity.class, saleSheet.getId());
		message = "销售申请单删除成功";
		saleSheetService.delete(saleSheet);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加销售申请单
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(SaleSheetEntity saleSheet, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(saleSheet.getId())) {
			message = "销售申请单更新成功";
			SaleSheetEntity t = saleSheetService.get(SaleSheetEntity.class, saleSheet.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(saleSheet, t);
				saleSheetService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "销售申请单更新失败";
			}
		} else {
			message = "销售申请单添加成功";
			saleSheetService.save(saleSheet);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 销售申请单列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(SaleSheetEntity saleSheet, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(saleSheet.getId())) {
			saleSheet = saleSheetService.getEntity(SaleSheetEntity.class, saleSheet.getId());
			req.setAttribute("saleSheetPage", saleSheet);
		}
		return new ModelAndView("yongxin/saleSheet/saleSheet");
	}
	
	
	/*
	 * 审批通过
	 * 
	 */
	@RequestMapping(params = "checkSelect")	
	@ResponseBody
    public AjaxJson checkSelect(HttpServletRequest req){
		AjaxJson j = new AjaxJson();
		
		String ids = req.getParameter("ids");
		String[] a = ids.split(",");
		
		if(a.length > 1){
			for(int i = 0;i < a.length;i++){
			String sql = " update t_sale_sheet set fstate = '1' where id = ? ";
			systemService.executeSql(sql,a[i]);
			}
		}else{
			String sql = " update t_sale_sheet set fstate = '1' where id = ? ";
			systemService.executeSql(sql,a[0]);
		}
		
		int row = 1;
		if(row > 0){
			message = "销售申请单审批成功";
		}else{
			message = "销售申请单审批失败";
		}
		j.setMsg(message);
		return j;
	}
   
	
	/*
	 * 审批不通过
	 * 
	 */
	@RequestMapping(params = "unCheckSelect")	
	@ResponseBody
    public AjaxJson unCheckSelect(HttpServletRequest req){
		AjaxJson j = new AjaxJson();
		
		String ids = req.getParameter("ids");
		String[] a = ids.split(",");
		
		if(a.length > 1){
			for(int i = 0;i < a.length;i++){
			String sql = " update t_sale_sheet set fstate = '2' where id = ? ";
			systemService.executeSql(sql,a[i]);
			}
		}else{
			String sql = " update t_sale_sheet set fstate = '2' where id = ? ";
			systemService.executeSql(sql,a[0]);
		}
		
		int row = 1;
		if(row > 0){
			message = "销售申请单审批成功";
		}else{
			message = "销售申请单审批失败";
		}
		j.setMsg(message);
		return j;
	}
}
